package com.jic.manager.controller.manage.sys;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.common.util.BeanUtil;
import com.jic.manager.entity.Mestore;
import com.jic.manager.entity.SysDept;
import com.jic.manager.entity.SysUserDept;
import com.jic.manager.request.*;
import com.jic.manager.request.orgcenter.SysDeptTreeRequest;
import com.jic.manager.response.*;
import com.jic.manager.service.MestoreService;
import com.jic.manager.service.SysDeptService;
import com.jic.manager.utils.UccHttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 机构控制器
 *
 * @autho wwb
 */
@Api(tags = "组织架构Controller")
@RestController
@Slf4j
@RequestMapping("dept")
public class SysDeptController implements SysDeptApi {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private UccHttpUtils uccHttpUtils;
    @Autowired
    private MestoreService mestoreService;

    @PostMapping(value = "/findDeptTree")
    public RestResult<List<SysDeptResponse>> findDeptTree(@RequestBody SysDeptRequest sysDeptRequest) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(sysDeptRequest, sysDept);
        sysDept.setDeleteFlag(0);
        return sysDeptService.findDeptTree(sysDept);
    }

    @PostMapping(value = "/queryDeptList")
    public RestResult<List<SysDept>> queryDeptList(@RequestBody SysDeptRequest sysDeptRequest) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(sysDeptRequest, sysDept);
        sysDept.setPid(sysDeptRequest.getAdminOperate().getDeptId());
        sysDept.setId(sysDeptRequest.getAdminOperate().getDeptId());
        List<SysDept> sysDepts = sysDeptService.query(sysDept);
        return RestResult.success(sysDepts);
    }

    /**
     * 查询当前登录平台以及所属组织
     *
     * @param sysDeptRequest
     * @return
     */
    @PostMapping(value = "/queryDeptListByDeptIds")
    public RestResult<List<SysDept>> queryDeptListByDeptIds(@RequestBody SysDeptRequest sysDeptRequest) {
        List<Long> ids = sysDeptRequest.getAdminOperate().getDeptIds();
        List<SysDept> list = new ArrayList<>();
        for (Long id : ids) {
            SysDept sysDept1 = sysDeptService.queryByPrimaryKey(id);
            if (sysDept1 != null) {
                list.add(sysDept1);
            }
        }
        return RestResult.success(list);
    }

    @PostMapping(value = "/editDeptInfo")
    public RestResult<String> editDeptInfo(@RequestBody SysDeptRequest sysDeptRequest) {
        SysDept sysDept = new SysDept();
        BeanUtils.copyProperties(sysDeptRequest, sysDept);
        return sysDeptService.editDeptInfo(sysDept);
    }

    @PostMapping(value = "deleteDept")
    public RestResult<String> deleteDept(@RequestBody SysDeptRequest sysDeptRequest) {
        return sysDeptService.deleteDept(sysDeptRequest.getIds());
    }

    @ApiOperation("批量插入组织架构-子平台 商家")
    @Override
    public RestResult<Boolean> saveDeptList(SaveDeptListRequest saveDeptListRequest) {
        return sysDeptService.saveDeptList(saveDeptListRequest);
    }

    @ApiOperation("查询用户拥有的数据权限")
    @PostMapping("/getUccUserAuthor")
    public RestResult<List<Long>> getUccUserAuthor(@RequestBody SysUserDept sysUserDept) {
        return sysDeptService.getUccUserAuthor(sysUserDept);
    }

    @ApiOperation("根据父级id查询下面所有子级的数据")
    @PostMapping("/getChilderByPid")
    public RestResult<List<Long>> getChilderByPid(@RequestBody SysDept sysDept) {
        return sysDeptService.getChilderByPid(sysDept);
    }

    @ApiOperation("修改用户权限")
    @PostMapping("/updateUserAuthor")
    public RestResult<Boolean> updateUserAuthor(@RequestBody UserAuthorRequest uccUserAuthorRequest) {
        try {
            return sysDeptService.updateUserAuthor(uccUserAuthorRequest);
        } catch (RuntimeException e) {
            log.error("修改用户权限错误....." + e.getLocalizedMessage());
            return RestResult.error("9999", "服务器错误...");
        }
    }

    @ApiOperation("修改组织名称")
    @PostMapping("/updateDeptName")
    public RestResult<Boolean> updateDeptName(@RequestBody UpdateDeptNameRequest updateDeptNameRequest) {
        return sysDeptService.updateDeptName(updateDeptNameRequest);
    }

    @ApiOperation("组织架构管理-查询组织架构树")
    @PostMapping(value = "/getAuthorDeptTree")
    public RestResult<List<SysDeptResponse>> getAuthorDeptTree(@RequestBody SysDeptTreeRequest sysDeptTreeRequest) {
        return sysDeptService.getAuthorDeptTree(sysDeptTreeRequest);
    }

    @ApiOperation("组织架构管理-查询组织架构树根据Pid")
    @PostMapping(value = "/getChildeListrByPid")
    public RestResult<List<SysDeptResponse>> getChildeListrByPid(@RequestBody SysDeptTreeRequest sysDeptTreeRequest) {
        return sysDeptService.getChildeListrByPid(sysDeptTreeRequest.getDeptId());
    }

    @PostMapping("/queryDeptNameById")
    @Override
    public RestResult<String> queryDeptNameById(@RequestParam("deptId") Long deptId) {
        return sysDeptService.queryDeptNameById(deptId);
    }

    @Override
    public List<Long> queryChildListById(@RequestBody Long deptId) {
        return sysDeptService.queryChildListById(deptId);
    }

    @ApiOperation("会员管理-修改成长值适用范围")
    @PostMapping(value = "/updateMerchantScopeApplicationById")
    @Override
    public RestResult<Long> updateMerchantScopeApplicationById(@RequestBody MerchantRequest merchantRequest) {
        return sysDeptService.updateMerchantScopeApplicationById(merchantRequest);
    }

    @ApiOperation("会员管理-查询成长值适用范围")
    @PostMapping(value = "/getGrowhtScopeInfo")
    @Override
    public MemMerchantInfoResponse getGrowhtScopeInfo(@RequestBody Long merchantId) {
        return sysDeptService.getGrowhtScopeInfo(merchantId);
    }

    @Override
    @PostMapping("querymMerchantOrStoreListByName")
    public List<Long> querymMerchantOrStoreListByName(String name) {
        return sysDeptService.querymMerchantOrStoreListByName(name);
    }

    @Override
    @PostMapping("getDeptIdByMsName")
    public RestResult<Long> getDeptIdByMsName(String msName) {
        return sysDeptService.getDeptIdByMsName(msName);
    }

    @Override
    @PostMapping("queryChildNameByDeptId")
    public RestResult<String> queryChildNameByDeptId(@RequestBody ChildNameRequest childNameRequest) {
        return sysDeptService.queryChildNameByDeptId(childNameRequest);
    }

 /*   @Override
    @PostMapping("querymMerchantOrStoreListById")
    public List<Long> querymMerchantOrStoreListById(Long deptId) {
        return null;
    }*/

    @Override
    @PostMapping("query")
    public List<SysDeptResponse> query(SysDeptRequest sysDept) {
        SysDept memberRequest = BeanUtil.copy(sysDept, SysDept.class);
        return sysDeptService.query(memberRequest).stream().map(x -> {
            SysDeptResponse response = new SysDeptResponse();
            BeanUtils.copyProperties(x, response);
            return response;
        }).collect(Collectors.toList());
    }


    @Override
    @PostMapping("getDeptIdByMsId")
    public Long getDeptIdByMsId(@RequestBody SysDeptRequest sysDept) {
        SysDeptRequest sysDept1 = new SysDeptRequest();
        sysDept1.setDeptType(sysDept.getDeptType());
        sysDept1.setId(sysDept.getId());
        return sysDeptService.getDeptIdByMsId(sysDept1);
    }

    @Override
    @PostMapping("getDeptList")
    public RestResult<List<SysDeptResponse>> getDeptList(SysDeptTreeRequest sysDeptTreeRequest) {
        return sysDeptService.getDeptList(sysDeptTreeRequest);
    }

    @Override
    public RestResult<List<Long>> getUccUserAuthorNew(Long userId) {
        return sysDeptService.getUccUserAuthor(new SysUserDept() {{
            setUserId(userId);
            setDeptType(1);
        }});
    }

    @Override
    public RestResult<List<Long>> getDeptIdsByStroreIdOrMerchantIdAndType(List<Long> businessIds, Integer deptType) {
        return RestResult.success(sysDeptService.getDeptIdsByStroreIdOrMerchantIdAndType(businessIds, deptType));
    }

    @Override
    public UccUserOutputDTO queryUserInfoByAccessToken(String accessToken) {
        UccUserOutputDTO uccUserOutputDTO = null;
        try {
            String userInfoByAccessToken = uccHttpUtils.getUserInfoByAccessToken(accessToken);
            uccUserOutputDTO = JSONObject.parseObject(userInfoByAccessToken, UccUserOutputDTO.class);
        } catch (Exception e) {
            log.error("queryUserInfoByAccessToken error, e = , accessToken = {}", e, accessToken);
        }
        return uccUserOutputDTO;
    }

    @Override
    @PostMapping(value = "/getStoreInfoByDeptId")
    public RestResult<MemStoreResponse> getStoreInfoByDeptId(SysDeptRequest sysDeptRequest) {
        return sysDeptService.getStoreInfoByDeptId(sysDeptRequest.getId());
    }

    @Override
    @PostMapping(value = "/getMerchantByDeptId")
    public RestResult<MerchantResponse> getMerchantByDeptId(SysDeptRequest sysDeptRequest) {
         return sysDeptService.getMerchantByDeptId(sysDeptRequest.getId());
    }
    @Override
    @GetMapping(value = "/getId")
    public SysDeptResponse getByIdPid(@RequestParam("id") Long id) {
        SysDeptResponse response=new SysDeptResponse();
        SysDept byIdPid = sysDeptService.getByIdPid(id);
        BeanUtils.copyProperties(byIdPid,response);
        return response;
    }

    @Override
    public RestResult<List<MemStoreResponse>> getStoreInfo() {
        Mestore mestore = new Mestore();
        mestore.setDeleteFlag(0);
        List<Mestore> mestoreList = mestoreService.query(mestore);
        List<MemStoreResponse> memStoreResponseList = mestoreList.stream().map(x -> {
            MemStoreResponse response = new MemStoreResponse();
            BeanUtils.copyProperties(x, response);
            return response;
        }).collect(Collectors.toList());
        return RestResult.success(memStoreResponseList);
    }




}
